package com.north.light.libpermission.impl;

import android.app.Activity;
import android.content.Context;

import androidx.fragment.app.Fragment;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.XXPermissions;
import com.north.light.libpermission.api.PermissionCallback;
import com.north.light.libpermission.api.PermissionImplApi;
import com.north.light.libpermission.constant.PermissionResultConstant;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * FileName: XXPermissionImpl
 * Author: lzt
 * Date: 2022/8/17 14:35
 */
public class XXPermission implements PermissionImplApi {


    @Override
    public boolean isGrant(Context context, String permission) {
        return XXPermissions.isGranted(context, permission);
    }

    @Override
    public boolean isGrant(Context context, String[] permission) {
        return XXPermissions.isGranted(context, permission);
    }

    @Override
    public boolean isGrant(Context context, List<String> permission) {
        return XXPermissions.isGranted(context, permission);
    }

    @Override
    public void requestPermission(Activity activity, String[] permission, int type, boolean enterSetting, PermissionCallback callback) {
        List<String> trainList = new ArrayList<>(Arrays.asList(permission));
        requestPermission(activity, trainList, type, enterSetting, callback);
    }

    @Override
    public void requestPermission(Activity activity, List<String> permission, int type, boolean enterSetting, PermissionCallback callback) {
        if (activity.isFinishing()) {
            return;
        }
        XXPermissions.with(activity)
                .permission(permission)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (callback != null) {
                            callback.result(type, PermissionResultConstant.success, all);
                            callback.result(type, PermissionResultConstant.success, permissions, all);
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (callback != null) {
                            if (never) {
                                callback.result(type, PermissionResultConstant.refuse_forever, false);
                                callback.result(type, PermissionResultConstant.refuse_forever, permissions, false);
                                //永久拒绝
                                if (enterSetting) {
                                    XXPermissions.startPermissionActivity(activity, permissions);
                                }
                            } else {
                                //不是永久
                                callback.result(type, PermissionResultConstant.refuse_once, false);
                                callback.result(type, PermissionResultConstant.refuse_once, permissions, false);
                            }
                        }
                    }
                });
    }

    @Override
    public void requestPermission(Fragment fragment, String[] permission, int type, boolean enterSetting, PermissionCallback callback) {
        List<String> trainList = new ArrayList<>(Arrays.asList(permission));
        requestPermission(fragment, trainList, type, enterSetting, callback);
    }

    @Override
    public void requestPermission(Fragment fragment, List<String> permission, int type, boolean enterSetting, PermissionCallback callback) {
        XXPermissions.with(fragment)
                .permission(permission)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (callback != null) {
                            callback.result(type, PermissionResultConstant.success, all);
                            callback.result(type, PermissionResultConstant.success, permissions, all);
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (callback != null) {
                            if (never) {
                                callback.result(type, PermissionResultConstant.refuse_forever, false);
                                callback.result(type, PermissionResultConstant.refuse_forever, permissions, false);
                                //永久拒绝
                                if (enterSetting) {
                                    XXPermissions.startPermissionActivity(fragment, permissions);
                                }
                            } else {
                                //不是永久
                                callback.result(type, PermissionResultConstant.refuse_once, false);
                                callback.result(type, PermissionResultConstant.refuse_once, permissions, false);
                            }
                        }
                    }
                });
    }

    @Override
    public void requestPermission(android.app.Fragment fragment, String[] permission, int type, boolean enterSetting, PermissionCallback callback) {
        List<String> trainList = new ArrayList<>(Arrays.asList(permission));
        requestPermission(fragment, trainList, type, enterSetting, callback);
    }

    @Override
    public void requestPermission(android.app.Fragment fragment, List<String> permission, int type, boolean enterSetting, PermissionCallback callback) {
        XXPermissions.with(fragment)
                .permission(permission)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (callback != null) {
                            callback.result(type, PermissionResultConstant.success, all);
                            callback.result(type, PermissionResultConstant.success, permissions, all);
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (callback != null) {
                            if (never) {
                                callback.result(type, PermissionResultConstant.refuse_forever, false);
                                callback.result(type, PermissionResultConstant.refuse_forever, permissions, false);
                                //永久拒绝
                                if (enterSetting) {
                                    XXPermissions.startPermissionActivity(fragment, permissions);
                                }
                            } else {
                                //不是永久
                                callback.result(type, PermissionResultConstant.refuse_once, false);
                                callback.result(type, PermissionResultConstant.refuse_once, permissions, false);
                            }
                        }
                    }
                });
    }
}
